package com.jinke.api.modules.ums.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.modules.ums.dto.ParcelTemplateAddDto;
import com.jinke.api.modules.ums.dto.ParcelTemplateUpdateDto;
import com.jinke.api.modules.ums.mapper.ParcelTemplateMapper;
import com.jinke.api.modules.ums.model.ParcelTemplate;
import com.jinke.api.modules.ums.model.UmsAdmin;
import com.jinke.api.modules.ums.service.IParcelTemplateService;
import com.jinke.api.modules.ums.vo.ParcelTemplateListVo;
import com.jinke.api.security.util.AdminUserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huang
 * @data 2024-05-23
 */
@Service
public class ParcelTemplateServiceImpl extends ServiceImpl<ParcelTemplateMapper, ParcelTemplate> implements IParcelTemplateService {
    @Override
    public CommonResult<List<ParcelTemplateListVo>> getListByUserId(String keyWord) {
        List<ParcelTemplateListVo> vos = new ArrayList<>();
        List<ParcelTemplate> list = lambdaQuery()
                .eq(ParcelTemplate::getUserId, AdminUserUtil.getUserId())
                .like(StringUtils.isNotEmpty(keyWord), ParcelTemplate::getName, keyWord)
                .orderByDesc(ParcelTemplate::getUpdateTime)
                .list();
        if (CollectionUtil.isNotEmpty(list)) {
            vos = list.stream().map(p -> {
                ParcelTemplateListVo vo = new ParcelTemplateListVo();
                BeanUtils.copyProperties(p, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return CommonResult.success(vos);
    }

    @Override
    public CommonResult updateByDto(ParcelTemplateUpdateDto dto) {
        ParcelTemplate template = lambdaQuery()
                .eq(ParcelTemplate::getUserId, AdminUserUtil.getUserId())
                .eq(ParcelTemplate::getName, dto.getName())
                .one();
        if (ObjectUtils.isNotEmpty(template) && !template.getId().equals(dto.getId())) {
            return CommonResult.failed("Name duplication");
        }
        ParcelTemplate parcelTemplate = lambdaQuery()
                .eq(ParcelTemplate::getId, dto.getId())
                .eq(ParcelTemplate::getUserId, AdminUserUtil.getUserId())
                .one();
        if (ObjectUtils.isEmpty(parcelTemplate)) {
            return CommonResult.failed("Permission denied");
        }
        BeanUtils.copyProperties(dto, parcelTemplate);
        updateById(parcelTemplate);
        return CommonResult.success();
    }

    @Override
    public CommonResult add(ParcelTemplateAddDto dto) {
        ParcelTemplate parcelTemplate = lambdaQuery()
                .eq(ParcelTemplate::getName, dto.getName())
                .eq(ParcelTemplate::getUserId, AdminUserUtil.getUserId())
                .one();
        if (ObjectUtils.isNotEmpty(parcelTemplate)) {
            return CommonResult.failed("Name duplication");
        }
        ParcelTemplate template = new ParcelTemplate();
        BeanUtils.copyProperties(dto, template);
        UmsAdmin userInfo = AdminUserUtil.getUserInfo();
        template.setUserId(userInfo.getId());
        template.setUpdateBy(userInfo.getUsername());
        template.setCreateBy(userInfo.getUsername());
        save(template);
        return CommonResult.success();
    }
}
